package com.itheima.app.cotroller;

import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.manager.MessageManager;
import com.itheima.domain.vo.HuanXinVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
public class MessageController {

    @Autowired
    private MessageManager messageManager;

    //用户认证，获取当前用户的用户名和密码，用于手机端的和环信云之间的通信
    @GetMapping("/huanxin/user")
    public ResponseEntity huanxinAuthc() {
        /*
         * 获取用户名字，密码
         * 封装 vo，返回*/

        Long userId = UserHolder.get().getId();
        HuanXinVo huanXinVo = new HuanXinVo();
        huanXinVo.setUsername(userId.toString());
        huanXinVo.setPassword("123456");

        return ResponseEntity.ok(huanXinVo);
    }

    //打招呼，给环信云发消息，相当于数据库管理员
    @PostMapping("/tanhua/strangerQuestions")
    public void sendMessage(@RequestBody Map<String, String> map) {
        String userId = map.get("userId");
        String reply = map.get("reply");
        messageManager.sendMessage(userId, reply);
    }

    //聊一下，联系人添加
    @PostMapping("/messages/contacts")
    public void addFriend(@RequestBody Map<String, String> map) {
        String friendId = map.get("userId");
        messageManager.addFriend(friendId);
    }

    //展示好友列表
    @GetMapping("/messages/contacts")
    public ResponseEntity findContacts(
            @RequestParam(value = "page",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize" ,defaultValue = "10") Integer pageSize) {
        return messageManager.findContacts(pageNum, pageSize);
    }

    //喜欢数
    @GetMapping("/messages/loves")
    public ResponseEntity findLoveNum(
            @RequestParam(value = "page",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize" ,defaultValue = "10") Integer pageSize) {
        Integer commentType = 3;
        return messageManager.findLikeNum(pageNum, pageSize, commentType);
    }

    //点赞数
    @GetMapping("/messages/likes")
    public ResponseEntity findLikesNum(
        @RequestParam(value = "page" , defaultValue = "1") Integer pageNum,
        @RequestParam(value = "pagesize" ,defaultValue = "10") Integer pageSize){

        Integer commentType = 1;
        return messageManager.findLikeNum(pageNum, pageSize, commentType);
    }

    //评论数
    @GetMapping("/messages/comments")
    public ResponseEntity findCommentNum(
            @RequestParam(value = "page" ,defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize" ,defaultValue = "10") Integer pageSize){
        Integer commentType = 2;
        return messageManager.findLikeNum(pageNum, pageSize, commentType);
    }

    //公告列表
    @GetMapping("/messages/announcements")
    public ResponseEntity findAnnouncement(
            @RequestParam(value = "page" , defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize" ,defaultValue = "10") Integer pageSize){
        return messageManager.findAnnouncement(pageNum,pageSize);
    }

}
